iT邦幫忙

2021 iThome 鐵人賽

DAY 19
0
Mobile Development

就是從無到有寫app系列 第 19

第19天~dialog+Menu

  • 分享至 

  • xImage
  •  

dialog的DEMO

掀開新檔

id命名為btnalert

https://ithelp.ithome.com.tw/upload/images/20220203/20119035yymi9Rz5iw.png

onClick從java檔來加入
https://ithelp.ithome.com.tw/upload/images/20220203/20119035tqBAxbX1Yk.png

再來是比較難的code

package com.huang.alert_dialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
    Button alert;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        alert = (Button) findViewById(R.id.btnalert);
        alert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();

                alertDialog.setTitle("靠自己好嗎?");
                alertDialog.setMessage("穩步踏實,則百事方佳~~");
                alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialogInterface, int i) {
                                dialogInterface.dismiss();
                            }
                        }


                );

                alertDialog.show();
            }
        });
    }
}

https://ithelp.ithome.com.tw/upload/images/20220203/201190355D8zsGOHva.png


Menu-就是手機上面的"三個點"

2種方法-這裡寫第1種用xml檔-layout+menu.xml

新增檔案-

https://ithelp.ithome.com.tw/upload/images/20220203/20119035jY1qMQleS4.png

再從res-new-Android Resource Directory
https://ithelp.ithome.com.tw/upload/images/20220203/20119035r8D8jydrfd.png

然後選type-選menu-上面會自己變

https://ithelp.ithome.com.tw/upload/images/20220203/20119035F0nC7diiiO.png

res夾–> 右 -->新增 resourse dir --> type:menu --> new resource file

menu/menu_main.xml
然後再從menu的右鍵去new-Menu Resource File

https://ithelp.ithome.com.tw/upload/images/20220203/20119035MYhWXbS1J8.png

制定名稱-

https://ithelp.ithome.com.tw/upload/images/20220203/20119035SzBVCu3Q9x.png

長這樣~

https://ithelp.ithome.com.tw/upload/images/20220203/20119035oravcJJAnO.png


整個就是menu-還有裡面的button

https://ithelp.ithome.com.tw/upload/images/20220203/20119035nuGXSfq1R2.png

拉入Menu Item

https://ithelp.ithome.com.tw/upload/images/20220203/20119035grT9e9JbCS.png

id+title+icon都要做

https://ithelp.ithome.com.tw/upload/images/20220203/20119035p25nzXBb9v.png
顯示在哪的
showAsAction="always"
要用空格點2下會挑出可以V~

https://ithelp.ithome.com.tw/upload/images/20220203/20119035I1O5D6RKK3.png

設置 showAsAction

ifRoom:如果 toolbar 上有位置才顯示,不然會在三個點點擊之後才出現

never:不顯示在界面上,只在三個點點擊之後才出現

always:會顯示在 toolbar 上
https://ithelp.ithome.com.tw/upload/images/20220203/20119035laGrxnZBI5.png

先看到icon再看到title
如果沒有icon就會直接看到title

這裡反而是手打程式碼不會動~是要按格子

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/Item01"
        android:icon="@android:drawable/btn_star_big_on"
        android:title="AA"
        app:showAsAction="always" />
</menu>


visible是否顯示

enabled是否可用
https://ithelp.ithome.com.tw/upload/images/20220203/20119035d1L1v11Ohp.png


再寫第2個沒有寫always就會被收在第2個
https://ithelp.ithome.com.tw/upload/images/20220203/20119035tZ6bjXP5jr.png

https://ithelp.ithome.com.tw/upload/images/20220203/20119035UP5TtbmjAN.png

選擇 ifRoom
https://ithelp.ithome.com.tw/upload/images/20220203/20119035lNCmUQdQwV.png

目前顯示
https://ithelp.ithome.com.tw/upload/images/20220203/20119035Kb9lwtO9VK.png
ifRoom:如果 toolbar 上有位置才顯示,不然會在三個點點擊之後才出現

never:不顯示在界面上,只在三個點點擊之後才出現

always:會顯示在 toolbar 上


第3個選never

https://ithelp.ithome.com.tw/upload/images/20220203/201190356IxtoVDrT2.png

https://ithelp.ithome.com.tw/upload/images/20220203/201190352ukO4h9vFU.png

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/Item01"
        android:icon="@android:drawable/btn_star_big_on"
        android:title="AA"
        app:showAsAction="always" />
    <item
        android:id="@+id/Item02"
        android:icon="@android:drawable/ic_btn_speak_now"
        android:title="BB"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/Item03"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="CC"
        app:showAsAction="never" />
</menu>

https://ithelp.ithome.com.tw/upload/images/20220203/201190358YdF4uJb30.png


如果沒有特別寫就是showAsAction="never"

https://ithelp.ithome.com.tw/upload/images/20220203/201190358U2us6J9X3.png

然後item04下面再放menu讓他一層又一層

https://ithelp.ithome.com.tw/upload/images/20220203/201190356Gu7Jb1cxq.png

顯示
https://ithelp.ithome.com.tw/upload/images/20220203/20119035NxUFBIkjMr.png

下面第1個item寫-

https://ithelp.ithome.com.tw/upload/images/20220203/20119035kgz2SUeiOb.png

下面第2個item寫-

https://ithelp.ithome.com.tw/upload/images/20220203/201190358t5WvWsQnB.png

程式碼:


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/Item01"
        android:icon="@android:drawable/btn_star_big_on"
        android:title="AA"
        app:showAsAction="always" />
    <item
        android:id="@+id/Item02"
        android:icon="@android:drawable/ic_btn_speak_now"
        android:title="BB"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/Item03"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="CC"
        app:showAsAction="never" />
    <item
        android:id="@+id/Item04"
        android:title="OTHER" >
        <menu >
            <item
                android:id="@+id/sub01"
                android:title="DD01" />
            <item
                android:id="@+id/sub02"
                android:title="DD02" />
        </menu>
    </item>
</menu>

目前模擬器是看不到東西的


從java檔讓menu顯示-右鍵Generate-->Override

安卓快捷鍵
https://medium.com/mindorks/11-android-studio-shortcuts-every-android-developer-must-know-a153e736e611

Override Methods(CTRL+O) :去尋找onCreateOptionsMenu(menu)-在WIN上

https://ithelp.ithome.com.tw/upload/images/20220203/20119035uWRdV74Raz.png

找到了~

https://ithelp.ithome.com.tw/upload/images/20220203/20119035JyVDdBpqeR.png

package com.huang.mymenu;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //建立menu


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}

java檔讓menu顯示-右鍵Generate-->Override

Override Methods(CTRL+O) :去尋找 -在WIN上

https://ithelp.ithome.com.tw/upload/images/20220203/20119035IpRHdOawEj.png

https://ithelp.ithome.com.tw/upload/images/20220203/20119035a507AVurfW.png

我按完有點跑掉-這裡用手動調成這樣

package com.huang.mymenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //建立menu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }

    //menu加偵聽器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }


}

在xml產生+在java檔叫出來

建立 分析器:menu角色 / 位置

package com.huang.mymenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //建立menu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater =getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);

        return true;
    }

    //menu加偵聽器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }


}

到這裡模擬器 就可以看到了喔~

https://ithelp.ithome.com.tw/upload/images/20220203/20119035NTvx7ND4CB.png

https://ithelp.ithome.com.tw/upload/images/20220203/20119035LCY8uOe1Ha.png


加偵聽器用switch

//menu加偵聽器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        
        switch (item.getItemId()){
            case R.id.Item01:
                Toast.makeText(MainActivity.this,"第1層第1項",Toast.LENGTH_SHORT).show();
                break;
        }
        
        
        return super.onOptionsItemSelected(item);
    }


item02 就是讓他直接消失,所以不用

Toast.makeText(MainActivity.this,"第1層第1項",Toast.LENGTH_SHORT).show();

https://ithelp.ithome.com.tw/upload/images/20220203/20119035s2k1IGejV0.png

程式碼改成:

case R.id.Item02:
                //Toast.makeText(MainActivity.this,"第1層第1項",Toast.LENGTH_SHORT).show();
                finish();

後面也是依序新增:


package com.huang.mymenu;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    //建立menu

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater =getMenuInflater();
        inflater.inflate(R.menu.menu_main,menu);

        return true;
    }

    //menu加偵聽器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()){
            case R.id.Item01:
                Toast.makeText(MainActivity.this,"第1層第1項",Toast.LENGTH_SHORT).show();
                break;
            case R.id.Item02:
                //Toast.makeText(MainActivity.this,"第1層第1項",Toast.LENGTH_SHORT).show();
                finish();

            case R.id.Item03:
                Toast.makeText(MainActivity.this,"第1層第3項",Toast.LENGTH_SHORT).show();
                break;

            case R.id.sub01:
                Toast.makeText(MainActivity.this,"第2層第1項",Toast.LENGTH_SHORT).show();
                break;
            case R.id.sub02:
                Toast.makeText(MainActivity.this,"第2層第2項",Toast.LENGTH_SHORT).show();
                break;

        }


        return super.onOptionsItemSelected(item); //賦予功能
    }


}


<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/Item01"
        android:icon="@android:drawable/btn_star_big_on"
        android:title="AA"
        app:showAsAction="always" />
    <item
        android:id="@+id/Item02"
        android:icon="@android:drawable/ic_btn_speak_now"
        android:title="BB"
        app:showAsAction="ifRoom" />
    <item
        android:id="@+id/Item03"
        android:icon="@android:drawable/ic_dialog_email"
        android:title="CC"
        app:showAsAction="never" />
    <item
        android:id="@+id/Item04"
        android:title="OTHER" >
        <menu >
            <item
                android:id="@+id/sub01"
                android:title="DD01" />
            <item
                android:id="@+id/sub02"
                android:title="DD02" />
        </menu>
    </item>
</menu>

因為是Toast所以也是要用 手機 模擬

https://ithelp.ithome.com.tw/upload/images/20220203/20119035fqwXrllYo9.jpg

https://ithelp.ithome.com.tw/upload/images/20220203/20119035B1EvbuP9z4.jpg

https://ithelp.ithome.com.tw/upload/images/20220203/20119035f0MfR716gd.jpg

https://ithelp.ithome.com.tw/upload/images/20220203/20119035PU8Lh3j8lW.jpg

https://ithelp.ithome.com.tw/upload/images/20220203/20119035BfR7T6DfIZ.jpg


用純java寫出來:

也是開新檔案

https://ithelp.ithome.com.tw/upload/images/20220203/20119035oPNUw5mrCC.png



import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        MenuItem item01 = menu.add(0,0,0,"AA");
        MenuItem item02 = menu.add(0,1,1,"BB");
        MenuItem item03 = menu.add(0,2,2,"CC");
        //MenuItem other = menu.add(0,3,3,"OTHER");
        item01.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

        SubMenu sub = menu.addSubMenu(0,3,3,"OTHER");
        sub.add(0,100, 0, "S1");
        sub.add(0,101, 1, "S2");

        return true;
    }

    //menu加偵聽器
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch(item.getItemId()){
            case 0:
                //Toast.makeText(MainActivity.this, "第一層第一項", Toast.LENGTH_SHORT).show();
                finish();
                break;
            case 1:
                Toast.makeText(MainActivity.this, "第一層第一項", Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MainActivity.this, "第一層第2項", Toast.LENGTH_SHORT).show();
                break;
            case 100:
                Toast.makeText(MainActivity.this, "第2層第一項", Toast.LENGTH_SHORT).show();
                break;
            case 101:
                Toast.makeText(MainActivity.this, "第2層第2項", Toast.LENGTH_SHORT).show();
                break;
        }

        return super.onOptionsItemSelected(item);
    }
}

因為是Toast所以也是要用 手機 模擬
https://ithelp.ithome.com.tw/upload/images/20220203/20119035JBFKn4dPWo.jpg

https://ithelp.ithome.com.tw/upload/images/20220203/20119035GXyLisqQNz.jpg

要上架的專案不能出現命名裡面有test或example


上一篇
第18天~SharedPreference常被使用於資料儲存
下一篇
第20天~Fragment
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言